package com.hnevc.mytrafficdemo.fragment;


import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.hnevc.mytrafficdemo.R;
import com.hnevc.mytrafficdemo.adapter.RechargeRecordAdapter;
import com.hnevc.mytrafficdemo.entity.RechargeRecord;
import com.hnevc.mytrafficdemo.util.db.RechargeRecordDBUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class RechargeRecordFragment extends Fragment {
    ListView mListView;
    FragmentActivity mContext;
    List<RechargeRecord> mRecordList;

    public RechargeRecordFragment() {
        // Required empty public constructor

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_recharge_record, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mContext  = getActivity();
        initData();
        mListView = (ListView) mContext.findViewById(R.id.id_lv_records);
        RechargeRecordAdapter adapter = new RechargeRecordAdapter(mContext,mRecordList);
        mListView.setAdapter(adapter);
    }

    /**
     * 初始化
     */
    private void initData() {
        mRecordList = new ArrayList<>();
        RechargeRecordDBUtil dbUtil = new RechargeRecordDBUtil(mContext);
        Cursor cursor = dbUtil.query();
        while(cursor.moveToNext()){
            RechargeRecord record = new RechargeRecord();
            record.setId(cursor.getInt(cursor.getColumnIndex("id")));
            record.setRecharger(cursor.getString(cursor.getColumnIndex("recharger")));
            record.setCarNum(cursor.getString(cursor.getColumnIndex("carNum")));
            record.setRechargeDate(cursor.getString(cursor.getColumnIndex("rechargeDate")));
            record.setRechargeAmount(cursor.getInt(cursor.getColumnIndex("rechargeAmount")));
            record.setBalance(cursor.getInt(cursor.getColumnIndex("balance")));
            mRecordList.add(record);
        }
        cursor.close();
    }
}
